1 Restarting work

1.1 Session

Restart session either by using GUI Session > Restart R or by Ctrl + Shift + F10. You should start with empty “Environment”. Tip: you can also clear your R console with Ctrl + L.

1.2 Packages

Load packages:

if (!require("tidyverse")) install.packages("tidyverse"); library("tidyverse")
if (!require("sf")) install.packages("sf"); library("sf")
if (!require("tmap")) install.packages("tmap"); library("tmap")
if (!require("sjmisc")) install.packages("sjmisc"); library("sjmisc")

Load data prepared during previous session:

SA2_SEIFA <- readRDS("data/SA2_SEIFA.Rds")

2 Mapping SEIFA indices in Melbourne - interactive maps

2.1 Quick map

We already use quck map qtm functionality of tmap library. We will extend its use by providing additional argument from linked SEIFA data:

tmap_mode("view")
## tmap mode set to interactive viewing
SA2_SEIFA %>% 
  qtm(fill = "IRSAD_d")

We get one more map of SA2s. This time it’s a thematic map! We mapped IRSAD_d variable to assign a colour that will fill polygon of SA2 using its particular value of decile.

2.2 Improving interactive map

Making quick maps can quickly become cumbersome when more options need to be specified to improve the map. We will switch to building maps layer by layer, similarly to ggplot package. Lets rewrite the code using more advanced syntax of tmap. We first define our base map using tm_shape function and then instruct tmap to treat it as layers of polygons with certain attributes:

tm_shape(SA2_SEIFA) +
  tm_polygons(col = "IRSAD_d", 
              n = 10, alpha = 0.7, palette = "RdYlGn", lwd = 0)

Can you guess what the parameters of tm_polygons are? Use help(tm_polygons) to learn about the one that you don’t know. Try changing a colour scheme of your map. Running code tmaptools::palette_explorer() will help guide your choices.

2.3 Faceted maps

tm_facets

tm_shape(SA2_SEIFA) +
  tm_polygons(col = c("IRSAD_d", "IER_d"), 
              n = 10, alpha = 0.7, palette = "RdYlGn", lwd = 0) +
  tm_facets(sync = TRUE, ncol = 2)

col is now c("IRSAD_d", "IER_d")

2.4 Proportional symbol map

tm_shape(SA2_SEIFA) +
  tm_borders(lwd = 0.5) +
  tm_bubbles(col = "IRSAD_d", size = "URP", 
             n = 10, alpha = 0.7, palette = "RdYlGn", border.lwd = 0, scale = 0.5)
## Legend for symbol sizes not available in view mode.

This type of map uses size of symbols to represent numeric variable on a map.

3 Mapping SEIFA indices in Melbourne - static maps

Interactive maps are great for exploration. On other occasions ‘static’ map can be a better option, for instance when you want to include it in printed report. We will learn how to create one with slightly more advanced use of tmap library functions.

3.1 Quick map

tmap_mode("plot")
## tmap mode set to plotting
tm_shape(SA2_SEIFA) +
  tm_polygons("IRSAD_d", title = "IRSAD deciles", 
              palette = "RdYlGn", n = 10, lwd = 0) 

First we instructed tmap package to change from interactive mode into static mode with tmap_mode() function. Notice that rest of parameters is the same as in interactive mode.

3.2 Improving static map

tm_shape(SA2_SEIFA) +
  tm_polygons("IRSAD_d", title = "IRSAD deciles", 
              palette = "RdYlGn", n = 10, lwd = 0,
              legend.hist = T) +
  tm_scale_bar(position = c("right", "bottom")) +
  tm_compass(position = c("left", "top"))+
  tm_layout(frame = F, 
            main.title = "IRSAD deciles of SA2 areas in Melbourne",
            main.title.size = 1.25, 
            main.title.position = c("left", "top"),
            legend.hist.size = 0.5, 
            legend.outside = T)

We have built a map layer by layer, calling different functions and specifying various arguments. We also improved the cartography by providing legend and a histogram (a graph of distribution of values). We specified our data source in tm_shape() function; many parameters indicating how polygons are displayed are specified in tm_polygons() function; we also added north arrow and scale with functions names appropriately; finally tm_layout helped us to put all small details together as legend and title).

Last but not least we saved our map can be saved as file on the disk. That can be done with a call tmap_save(tmap_last(), "path_to_save/name.png"). Notice that we used function inside a finction there to use last displayed map.

4 Further topics

  1. Try mapping indices other than IRSAD.

  2. Try creating static map with proportianl symbols.

  3. Try creating static maps with facets.